﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.ServiceModel;

/// <summary>
/// Summary description for BookEntity
/// </summary>
[DataContract]
[KnownType(typeof(List<BookDetailEntity>))]
public class BookEntity
{
	public BookEntity()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    [DataMember]
    public long ID { set; get; }

    [DataMember]
    public string Name { set; get; }

    [DataMember]
    public string Description { set; get; }

    [DataMember]
    public string Link { set; get; }

    [DataMember]
    public string Image { set; get; }

    [DataMember]
    public int? NumberOfDownload { set; get; }

    [DataMember]
    public string Author { set; get; }

    [DataMember]
    public DateTime? DateUpload { set; get; }

    [DataMember]
    public string UploadBy { set; get; }

    [DataMember]
    public bool? Active { set; get; }

    [DataMember]
    public long? CategoryID { set; get; }

    [DataMember]
    public string Type { set; get; }

    [DataMember]
    public int? NumberOfLike { set; get; }

    [DataMember]
    public BookDetail bookDetail { set; get;}
    [DataMember]
    public List<BookDetailEntity> Details { set; get; }

    [DataMember]
    public Category category { set; get; }    

    public BookEntity(Book book)
    {
        this.ID = book.ID;
        this.Name = book.Name;
        this.Description = book.Description;
        this.Link = book.Link;
        this.Image = book.Image;
        this.NumberOfDownload = book.NumberOfDownload;
        this.Author = book.Author;
        this.DateUpload = book.DateUpload;
        this.UploadBy = book.UploadBy;
        this.Active = book.Active;
        this.CategoryID = book.CategoryID;
        this.Type = book.Type;
        this.NumberOfLike = book.NumberOfLike;

        loadBookDetail();
        /*loadCategory();*/
    }

    /// <summary>
    /// Get BookDetail by current bookID
    /// </summary>
    /// <returns></returns>
    public void loadBookDetail()
    {
        try
        {
            BookContextDataContext bookContext = new BookContextDataContext();
            
            List<BookDetail> list = bookContext.BookDetails.Where(t => t.BookID == ID).ToList();
            Details=new List<BookDetailEntity>();
            foreach (BookDetail item in list)
            {
                Details.Add(new BookDetailEntity(item));
            }
        }
        catch
        {}
    }

    /// <summary>
    /// Get category by current bookId
    /// </summary>
    /// <returns></returns>
    public void loadCategory()
    {
        try
        {
            BookContextDataContext bookContext = new BookContextDataContext();
            category = bookContext.Categories.Where(c => c.ID == CategoryID).First();
        }
        catch
        { }
    }
}